package com.probox.modules.controller.product;

import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.domain.R;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.product.ProductBrand;
import com.probox.common.entity.service.product.IProductBrandService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.product.api.domain.ProductBrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品品牌Controller
 *
 *
 * @date 2021-05-25
 */
@RestController
@RequestMapping("/product/brand")
public class ProductBrandController extends BaseController {
    @Autowired
    private IProductBrandService productBrandService;

    /**
     * 查询商品品牌列表
     */
    @PreAuthorize(hasPermi = "product:brand:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductBrand productBrand) {
        startPage();
        List<ProductBrand> list = productBrandService.queryList(productBrand);
        return getDataTable(list);
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/bulidSelect")
    public AjaxResult bulidSelect()
    {
        return AjaxResult.success(productBrandService.bulidSelect());
    }



    /**
     * 导出商品品牌列表
     */
    @PreAuthorize(hasPermi = "product:brand:export")
    @Log(title = "导航栏目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductBrand productBrand) throws IOException {
        List<ProductBrand> list = productBrandService.queryList(productBrand);
        ExcelUtil<ProductBrand> util = new ExcelUtil<ProductBrand>(ProductBrand.class);
        util.exportExcel(response, list, "导航栏目数据");
    }

    /**
     * 获取商品品牌详细信息
     */
    @PreAuthorize(hasPermi = "product:brand:query")
    @GetMapping(value = "/{brandUid}")
    public AjaxResult getInfo(@PathVariable("brandUid") Long brandUid) {
        return AjaxResult.success(productBrandService.selectProductBrandById(brandUid));
    }

    /**
     * 新增商品品牌
     */
    @PreAuthorize(hasPermi = "product:brand:add")
    @Log(title = "导航栏目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductBrand productBrand) {
        return toAjax(productBrandService.insertProductBrand(productBrand));
    }

    /**
     * 修改商品品牌
     */
    @PreAuthorize(hasPermi = "product:brand:edit")
    @Log(title = "导航栏目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductBrand productBrand) {
        return toAjax(productBrandService.updateProductBrand(productBrand));
    }

    /**
     * 修改商品品牌状态
     */
    @PreAuthorize(hasPermi = "product:brand:edit")
    @Log(title = "导航栏目", businessType = BusinessType.UPDATE)
    @PutMapping("/updateBrandStatus")
    public AjaxResult changeStatus(@RequestBody ProductBrand productBrand) {
        return toAjax(productBrandService.updateProductBrand(productBrand));
    }

    /**
     * 删除商品品牌
     */
    @PreAuthorize(hasPermi = "product:brand:remove")
    @Log(title = "导航栏目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{brandUids}")
    public AjaxResult remove(@PathVariable Long[] brandUids) {
        return toAjax(productBrandService.deleteProductBrandByIds(brandUids));
    }


    @GetMapping(value = "list/{brandUid}")
    public R<ProductBrandVo> getBrand(@PathVariable("brandUid") Long brandUid) {
        ProductBrandVo productBrandApi = new ProductBrandVo();
        ProductBrand productBrand = productBrandService.selectProductBrandById(brandUid);
        if(ObjectUtil.isEmpty(productBrand)) return R.ok(productBrandApi);
        BeanUtils.copyProperties(productBrand, productBrandApi);
        return R.ok(productBrandApi);
    }
}
